package com.zbkj.admin.service;

import cn.hutool.core.util.BooleanUtil;
import com.zbkj.admin.model.req.SFExpressCloudPrintWaybills;
import com.zbkj.admin.model.req.SFExpressOrderState;
import com.zbkj.admin.vo.SFExpressOrderStateVo;
import com.zbkj.dal.base.enums.OrderInvoceStatus;
import com.zbkj.dal.base.enums.OrderSubStageType;
import com.zbkj.dal.base.enums.OrderType;
import com.zbkj.dal.common.enums.SFExpressOrderPushState;
import com.zbkj.dal.common.utils.JsonUtil;
import com.zbkj.dal.entity.express.ExpressCloudPrintWaybill;
import com.zbkj.dal.entity.order.Order;
import com.zbkj.dal.entity.order.OrderInvoice;
import com.zbkj.service.service.ExpressCloudPrintWaybillService;
import com.zbkj.service.service.OrderInvoiceService;
import com.zbkj.service.service.OrderService;
import com.zbkj.service.service.order.OrderRentRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SFExpressOrderStateService {


    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderInvoiceService orderInvoiceService;
    @Autowired
    private OrderRentRecordService orderRentRecordService;
    @Autowired
    private ExpressCloudPrintWaybillService expressCloudPrintWaybillService;

    public SFExpressOrderStateVo accept(SFExpressOrderState orderState) {
        try {
            log.info("消费订单状态推送: {}", JsonUtil.toJSONString(orderState));
            List<SFExpressOrderState.OrderState> orderStates = orderState.getOrderState();
            orderStates.forEach(orderStateItem -> {
                // 订单号 PT 单号
                final String orderNo = orderStateItem.getOrderNo();
                // 订单状态code
                String orderStateCode = orderStateItem.getOrderStateCode();
                SFExpressOrderPushState sfExpressOrderPushState = SFExpressOrderPushState.of(orderStateCode);
                // 处理通知
                accept(orderNo, sfExpressOrderPushState, orderStateItem);
            });
            return SFExpressOrderStateVo.success();
        } catch (Exception e) {
            return SFExpressOrderStateVo.error("");
        }
    }

    private void accept(String expressOrderNo, SFExpressOrderPushState state, SFExpressOrderState.OrderState item) {
        switch (state) {
            // 下单已经接收, 时候需要更新订单的物流状态，变更为已经发货状态
            case _40037:
                this.accept40037(expressOrderNo, item.getWaybillNo());
                break;
            // 订单已经取件
            case _40019:
                this.accept40019(expressOrderNo);
                break;
            // 正常收件状态
            case _40003:
                this.accept40003(expressOrderNo);
            default:
        }
    }

    /**
     * 已经创建订单回调
     *
     * @param expressOrderNo 物流定单号
     * @param waybillNo      物流单号
     */
    private void accept40037(String expressOrderNo, String waybillNo) {
        OrderInvoice orderInvoice = orderInvoiceService.findByExpressOrderNo(expressOrderNo);
        // 判断订单阶段类型
        OrderSubStageType orderSubStageType = OrderSubStageType.of(orderInvoice.getExpressOrderType());
        // 1.承享\买入支付订单
        if (Objects.equals(OrderSubStageType.RENT_CREATE, orderSubStageType)) {
            // 订单发货
        }
        // 2. 承享归还订单
        if (Objects.equals(OrderSubStageType.RENT_RETURN, orderSubStageType)) {
            // 需要更新 ExpressOrder 的状态，更新成待揽收
        }
        orderInvoice.setStatus(OrderInvoceStatus.WAITING.getCode());
        orderInvoiceService.updateById(orderInvoice);
    }

    /**
     * 已经取件 成功回调
     *
     * @param expressOrderNo 物流订单号
     */
    private void accept40019(String expressOrderNo) {
        OrderInvoice orderInvoice = orderInvoiceService.findByExpressOrderNo(expressOrderNo);
        orderInvoice.setStatus(OrderInvoceStatus.TRANSIT.getCode());
        orderInvoiceService.updateById(orderInvoice);
    }

    /**
     * 正常签收
     *
     * @param expressOrderNo 物流订单号
     */
    private void accept40003(String expressOrderNo) {
//        OrderInvoice orderInvoice = orderInvoiceService.findByExpressOrderNo(expressOrderNo);
//        orderInvoice.setStatus(OrderInvoceStatus.SIGNED.getCode());
//        orderInvoiceService.updateById(orderInvoice);
        // 如果是承享订单，开始设置【eb_order_rent_record】开始承享日期
//        Order order = orderService.getByOrderNo(orderInvoice.getOrderNo());
//        if (Objects.equals(OrderType.RENT.getCode(), order.getType())) {
//            if (BooleanUtil.isFalse(orderRentRecordService.updateInitialRentOrderStartOnSign(orderInvoice.getOrderNo()))) {
//                 发送消息，订单签收但是！，更新承享信息失败！
//                 TODO 预警消息 HY
//            }
//        }
//        takeDelivery(orderInvoice.getOrderNo());
    }

    public SFExpressOrderStateVo sfCloudPrintWaybillsPdf(SFExpressCloudPrintWaybills req) {
        try {
            String msgDataStr = req.getMsgData();
            SFExpressCloudPrintWaybills.MsgData msgData = JsonUtil.parseObject(msgDataStr, SFExpressCloudPrintWaybills.MsgData.class);
            Assert.notNull(msgData, "数据为空");
            System.out.println(msgData);
            // 新建对象
            ExpressCloudPrintWaybill expressCloudPrintWaybill = new ExpressCloudPrintWaybill();
            expressCloudPrintWaybill.setMailNo(msgData.getWaybillNo());
            expressCloudPrintWaybill.setContent(Base64Utils.decodeFromString(msgData.getContent()));
            expressCloudPrintWaybill.setPath("");
            expressCloudPrintWaybill.setFileName(msgData.getFileName());
            expressCloudPrintWaybill.setFileType(msgData.getFileType());
            expressCloudPrintWaybill.setSeqNo(msgData.getSeqNo());
            expressCloudPrintWaybill.setAreaNo(msgData.getAreaNo());
            expressCloudPrintWaybill.setPageNo(msgData.getPageNo());
            expressCloudPrintWaybill.setTemplateCode(msgData.getTemplateCode());
            expressCloudPrintWaybill.setEnable(1);
            expressCloudPrintWaybillService.createNew(expressCloudPrintWaybill);
        } catch (Exception e) {
            log.error("保存电子面单失败！", e);
        }
        return SFExpressOrderStateVo.success();
    }
}


